<?php
//blog
//april 5, 2008
// update april 9, 2008

//@todo: add anti-spam to comments

class Controller extends controllerBase {

	public $method = array(0 => 'view');

	public function view($id = "")
	{
		if($id != "")
		{
			#display a single entry (with comment form if logged in)
			
			#try to find it by ID
			$query = new Query("SELECT `id` FROM `blogs` WHERE `id`  = '" . $id ."' ORDER BY `id` LIMIT 0, 1");
			
			#if not found by ID, try to find by url
			if($query->num_rows() <= 0)
			{
				#we passed the url of the blog entry, see if it exists and find the id
				$query = new Query("SELECT `id` FROM `blogs` WHERE `url` = '" . $id ."' ORDER BY `id` LIMIT 0, 1");
			}
			
			#now we actually run the query
			
			
			#if the item exits, display it, else, error
			if($query->num_rows() > 0)
			{
				#the item exits, get it and display
				$p = $query->fetch_row();
				
				$blog = new blog($p[0]);
				
				#display the blog entry
				$show .= display::h2($blog->title)
					   . format::content($blog->content);
					  
				#get tags
				if($blog->getTags())
				{
					$show .= '<p>Tags: ';
					foreach($blog->tag as $tag)
					{
						$show .= display::link($tag->tag, 'tag/' . $tag->tag) . ', ';
					}
					$show .= ' </p>';
				}

				
				//get comments
				$show .= '<h3>Comments</h3>
					  ';
				
				if($blog->getComments())
				{
					foreach($blog->comment as $comment)
					{
						$show .= '<h4>' . $comment->author . ' - ' . format::date($comment->date) . '</h4>
							  <p>' . $comment->content . '</p>';
					}
				}
				else
				{
					$show .= '<p>There are currently no comments.</p>';
				}
				
				//$show .= form::addComment($errors = array(), $blog->id, 'blog');
				$show .= form::getForm('comment_add', array($errors = array(), $blog->id, 'blog'));

			}
			else
			{
				$show .= display::itemDNE('blog entry');
			}
			
			$this->title = $blog->title;
			$this->content = $show;
		}
		else
		{
			#display the first X entreis, really, this is the page method
			self::page(1);
		}
	}
	
	public function addComment($id)
	{
		#process the add comment form
		
		if(isset($_POST['submit']))
		{
			$submitted = false;
			$errors = array();
			
			$newComment = new comment();
			
			#check content
			if($_POST['content'] != '')
			{
				$newComment->content = $_POST['content'];
			}
			else
			{
				$errors[] = 'missing-content';
			}
			
			#check email address
			if(clean::checkEmail($_POST['email']))
			{
				$newComment->email = $_POST['email'];
			}
			else
			{
				$errors[] = 'missing-or-invalid-email';
			}
			
			#check display name
			if($_POST['name'] != '')
			{
				$newComment->author = $_POST['name'];
			}
			else
			{
				$errors[] = 'missing-name';
			}
			
			#get ip and date
			$newComment->ip = $_SERVER['REMOTE_ADDR'];
			$newComment->date = time();
			$newComment->blog = $id;
			
			#no errors, submit info
			if(count($errors) == 0)
			{
				$newComment->save();
				
				$submitted = true;
			}
			if($submitted)
			{
				#show redirect
				$show = display::redirect("Comment Submitted.", ROOT_URL . URL_BASE . "blog/" . $id);
			}
			else
			{
				#show form again
				$show .= form::getForm('comment_add', array($errors, $id, 'blog'));
			}
			
		}
		else
		{
			//they shouldnt be here
			$show = display::redirect("An error has occured.", ROOT_URL . URL_BASE . "blog/" . $id);
		}
		
		$this->content = $show;
		
	}
	
	
	public function page($page)
	{
		#displays a page of entries
		
		#get the starting point of entries to display
		$start = (($page - 1) * 5);
		
		$query = new Query("SELECT * FROM `blogs` WHERE `publishDate` <= ? ORDER BY `publishDate` DESC LIMIT ?, 5", time(), $start);
		
		if($query->num_rows() > 0)
		{
			$blogs = blog::RecordSet($query, 'blog');
			
			#display these blog entries
			foreach ($blogs as $blog)
			{
				$show .= display::headerLink($blog->data['title'], 'blog/' . $blog->data['url']) . 
				           display::dateHeader($blog->data['date']) . '
						  ' . format::short($blog->data['content']) . '
						   <p>' . display::link('Read Full Story/Add Comment', 'blog/' . $blog->data['url']) . '</p>';
				
				#get tags
				if($blog->getTags())
				{
					$show .= '<p>Tags: ';
					foreach($blog->tag as $tag)
					{
						$show .= display::link($tag->tag, 'tag/' . $tag->tag) . ', ';
					}
					$show .= ' </p>';
				}
			}
		}
		else
		{
			$show .= '<p> There are currently no blog entries.</p>';
		}
		
		
		$this->title = "Blogs";
		$this->content = $show;
		
		
	}
}
?>